package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.bean.codeMarketing.CodeMarketingInfo;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SoundManager;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.GuideRecordDialog;

import java.io.File;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeVoiceSetActivity extends BaseCodeFinishActivity {

        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_next)
        Button codeIvNext;

        @BindView(R.id.code_cb_1)
        ImageView codeCb1;

        @BindView(R.id.code_rl_1)
        RelativeLayout codeRl1;

        @BindView(R.id.code_cb_2)
        ImageView codeCb2;

        @BindView(R.id.code_rl_2)
        RelativeLayout codeRl2;

        @BindView(R.id.code_cb_3)
        ImageView codeCb3;

        @BindView(R.id.code_rl_3)
        RelativeLayout codeRl3;

        @BindView(R.id.code_jump_bt)
        ImageView codeJumpBt;

        private ToastTools toastTools;

        private int type;

        private String audioPath;

        private String content;

        private View ivOk;

        private GuideRecordDialog recodeDialog;

        private View ivPb;

        private TextView tvLoad;

        private AudioEvent recodeAudio;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_voice_module);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                Intent intentResult = getIntent();
                type = intentResult.getIntExtra("type", -1);
                audioPath = intentResult.getStringExtra("path");
                content = intentResult.getStringExtra("content");
                if (type != -1) {
                        setSelected(type);
                }
        }

        private void setSelected(int type) {
                codeCb1.setSelected(false);
                codeCb2.setSelected(false);
                codeCb3.setSelected(false);
                switch (type) {
                        case AudioEvent.AUDIO_TYPE_TTS:
                                codeCb1.setSelected(true);
                                break;
                        case AudioEvent.AUDIO_TYPE_AUDIO:
                                codeCb2.setSelected(true);
                                break;
                        case AudioEvent.AUDIO_TYPE_RECODE:
                                codeCb3.setSelected(true);
                                break;
                        default:
                                break;
                }
        }

        @OnClick({R.id.code_iv_back, R.id.code_iv_next, R.id.code_rl_1, R.id.code_rl_2, R.id.code_rl_3, R.id.code_jump_bt})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_next:
                                if (TextUtils.isEmpty(content) || type == -1) {
                                        toastTools.showText(getString(R.string.voice_cannot_empty));
                                        return;
                                }
                                CodeMarketingInfo codeMarketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                                codeMarketingInfo.setVoiceType(type);
                                codeMarketingInfo.setVoiceContent(audioPath);
                                startOtherActivity(CodeTimeActivity.class, false);
                                break;
                        case R.id.code_rl_1:
                                startOtherActivity(this,CodeTTsSetActivity.class, AudioEvent.AUDIO_TYPE_TTS);
                                break;
                        case R.id.code_rl_2:
                                startOtherActivity(this,  CodeChooseAudioActivity.class, AudioEvent.AUDIO_TYPE_AUDIO);
                                break;
                        case R.id.code_rl_3:
                                record();
                                break;
                        case R.id.code_jump_bt:
                                codeMarketingInfo = CodeDataHelper.getInstance().getCodeMarketingInfo();
                                codeMarketingInfo.setVoiceType(-1);
                                codeMarketingInfo.setVoiceContent(null);
                                startOtherActivity(CodeTimeActivity.class, false);
                                break;
                }
        }

        /**
         * 进入录音
         */
        private void record() {
                if (recodeDialog == null) {
                        //点击语音录入图标
                        recodeDialog = new GuideRecordDialog(this, new GuideRecordDialog.IRecodeListener() {
                                @Override
                                public void finish(String fileName) {
                                        Logger.e("test", "fileName" + fileName);
                                        if (TextUtils.isEmpty(fileName)) {
                                                toastTools.showText(getString(R.string.voice_cannot_empty));
                                                return;
                                        }
                                        Logger.d("SpeechSetupActivity", "录音的文件名：" + fileName);
                                        SoundManager.getInstance().playSound(PathConst.ROOT_PATH + PathConst.RECORD_FILE_PATH + fileName, null);
                                        showLoadDialog();
                                        recodeAudio = new AudioEvent(AudioEvent.AUDIO_TYPE_RECODE, PathConst.RECORD_FILE_PATH + fileName, fileName);
                                        sendMsg2Chest(recodeAudio);
                                }

                                @Override
                                public void cancel() {

                                }

                                @Override
                                public void error() {
                                        toastTools.showText(getString(R.string.recording_failure));
                                }
                        });
                }
                recodeDialog.show();

        }

        private Dialog loadDialog;

        private void showLoadDialog() {
                if (loadDialog == null) {
                        loadDialog = new Dialog(this, R.style.MyDialogStyle);
                        loadDialog.setContentView(R.layout.guide_dialog_load);
                        ivOk = loadDialog.findViewById(R.id.guide_load_iv);
                        ivPb = loadDialog.findViewById(R.id.guide_load_pb);
                        tvLoad = (TextView) loadDialog.findViewById(R.id.guide_load_tv);
                        loadDialog.setCancelable(false);
                }
                ivPb.setVisibility(View.VISIBLE);
                ivOk.setVisibility(View.INVISIBLE);
                tvLoad.setText("正在上传...");
                StatusBarUtil.hideVirtualKey(loadDialog);
                loadDialog.show();
        }

        private void sendMsg2Chest(final AudioEvent data) {
                String url = IpMsgConst.HTTP_URL + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 3 + "");
                hashMap.put("data", JSON.toJSONString(data));
                hashMap.put(data.getMusicName(), new File(PathConst.ROOT_PATH + data.getMusicPath()));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                                toastTools.showText(CodeVoiceSetActivity.this.getString(R.string.upload_complete));
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ivPb.setVisibility(View.INVISIBLE);
                                                ivOk.setVisibility(View.VISIBLE);
                                                tvLoad.setText(CodeVoiceSetActivity.this.getString(R.string.upload_complete));
                                                tvLoad.postDelayed(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                                loadDialog.dismiss();
                                                        }
                                                }, 1000);
                                                type = data.getAudioType();
                                                audioPath = data.getMusicPath();
                                                content = data.getMusicName();
                                                setSelected(type);
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText(getString(R.string.service_error_hint));
                                loadDialog.dismiss();
                        }
                });
        }

        /**
         * 跳转界面
         *
         * @param
         */
        protected void startOtherActivity(Context context, Class<?> cls, int code) {
                Intent intent = new Intent(context, cls);
                intent.putExtra("path", audioPath);
                intent.putExtra("content", content);
                intent.putExtra("type", type);
                startActivityForResult(intent, code);
                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (data == null || resultCode != RESULT_OK) {
                        return;
                }
                if (requestCode == AudioEvent.AUDIO_TYPE_AUDIO) {//音频
                        type = data.getIntExtra("type", -1);
                        audioPath = data.getStringExtra("path");
                        content = data.getStringExtra("content");
                }
                if (requestCode == AudioEvent.AUDIO_TYPE_TTS) {//文字
                        type = data.getIntExtra("type", -1);
                        content = data.getStringExtra("content");
                        audioPath = content;
                        if (TextUtils.isEmpty(content)) {
                                type = -1;
                        }
                }
                setSelected(type);
        }
}
